/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jj2000.stg.ui;

import java.awt.BorderLayout;
import java.awt.Image;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import jj2000.disp.BlkImgDataSrcImageProducer;
import jj2000.disp.ImgKeyListener;
import jj2000.disp.ImgScrollPane;
import jj2000.j2k.image.DataBlk;
import jj2000.j2k.image.DataBlkInt;
import jj2000.j2k.image.input.ImgReaderPPM;
import jj2000.stg.wt.HaarWT;

/**
 *
 * @author teq
 */
public class HaarDwtForm extends javax.swing.JFrame {

    public HaarDwtForm(String imgPath, String wtmarkPath) {
        
        this();
        
        imagePath = imgPath;
        watermarkPath = wtmarkPath;
        
        try {
            ImgReaderPPM imgRdr = new ImgReaderPPM(imgPath);

            Image img = BlkImgDataSrcImageProducer
                            .createImage(imgRdr, originalImageISP);
            originalImageISP.setImage(img);
            
        } catch (Exception ex) {
            MsgBox(ex.getMessage());
        }
    }

    /**
     * Creates new form HaarDwtForm
     */
    public HaarDwtForm() {
        
        initComponents();
        
        originalImageISP = new ImgScrollPane();
        jOriginalImgPanel.add(originalImageISP, BorderLayout.CENTER);
        originalImageISP.addKeyListener(new ImgKeyListener(originalImageISP));
        jOriginalImgPanel.validate();
        
        decomposedImageISP = new ImgScrollPane();
        jDecomposedImgPanel.add(decomposedImageISP, BorderLayout.CENTER);
        decomposedImageISP.addKeyListener(new ImgKeyListener(decomposedImageISP));
        jDecomposedImgPanel.validate();

        originalAndHiddenImageISP = new ImgScrollPane();
        jOriginaAndHiddenlImgPanel.add(originalAndHiddenImageISP, BorderLayout.CENTER);
        originalAndHiddenImageISP.addKeyListener(new ImgKeyListener(originalAndHiddenImageISP));
        jOriginaAndHiddenlImgPanel.validate();
        
        extractedImageISP = new ImgScrollPane();
        jExtractedImgPanel.add(extractedImageISP, BorderLayout.CENTER);
        extractedImageISP.addKeyListener(new ImgKeyListener(extractedImageISP));
        jExtractedImgPanel.validate();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDecomposeButton = new javax.swing.JButton();
        jOriginalImgPanel = new javax.swing.JPanel();
        jDecomposedImgPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jDwtLevelSpinner = new javax.swing.JSpinner();
        jstegBitsSpinner = new javax.swing.JSpinner();
        jOriginaAndHiddenlImgPanel = new javax.swing.JPanel();
        jExtractedImgPanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jDecomposeButton.setText("Decompose");
        jDecomposeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jDecomposeButtonActionPerformed(evt);
            }
        });

        jOriginalImgPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Original"));
        jOriginalImgPanel.setLayout(new java.awt.BorderLayout());

        jDecomposedImgPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Decomposed + hidden"));
        jDecomposedImgPanel.setLayout(new java.awt.BorderLayout());

        jLabel1.setText("Decomposition level");

        jDwtLevelSpinner.setModel(new javax.swing.SpinnerNumberModel(1, 1, 20, 1));

        jstegBitsSpinner.setModel(new javax.swing.SpinnerNumberModel(1, 0, 8, 1));

        jOriginaAndHiddenlImgPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Original + hidden"));
        jOriginaAndHiddenlImgPanel.setLayout(new java.awt.BorderLayout());

        jExtractedImgPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Extracted hidden image"));
        jExtractedImgPanel.setLayout(new java.awt.BorderLayout());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jOriginalImgPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 420, Short.MAX_VALUE)
                            .addComponent(jOriginaAndHiddenlImgPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jDecomposedImgPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 419, Short.MAX_VALUE)
                            .addComponent(jExtractedImgPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(12, 12, 12)
                        .addComponent(jDwtLevelSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jDecomposeButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jstegBitsSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jDecomposeButton)
                    .addComponent(jLabel1)
                    .addComponent(jDwtLevelSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jstegBitsSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jDecomposedImgPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE)
                    .addComponent(jOriginalImgPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jExtractedImgPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE)
                    .addComponent(jOriginaAndHiddenlImgPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jDecomposeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jDecomposeButtonActionPerformed

        try {
            ImgReaderPPM imgRdr0 = new ImgReaderPPM(imagePath);
            ImgReaderPPM imgRdr1 = new ImgReaderPPM(imagePath);
            ImgReaderPPM imgRdr2 = new ImgReaderPPM(imagePath);
            ImgReaderPPM wtmRdr0 = new ImgReaderPPM(watermarkPath);
            ImgReaderPPM wtmRdr1 = new ImgReaderPPM(watermarkPath);
            ImgReaderPPM wtmRdr2 = new ImgReaderPPM(watermarkPath);

            DataBlk imgBlk0 = new DataBlkInt(0, 0, imgRdr0.getImgWidth(), imgRdr0.getImgHeight());
            DataBlk imgBlk1 = new DataBlkInt(0, 0, imgRdr1.getImgWidth(), imgRdr1.getImgHeight());
            DataBlk imgBlk2 = new DataBlkInt(0, 0, imgRdr2.getImgWidth(), imgRdr2.getImgHeight());
            DataBlk wtmBlk0 = new DataBlkInt(0, 0, wtmRdr0.getImgWidth(), wtmRdr0.getImgHeight());
            DataBlk wtmBlk1 = new DataBlkInt(0, 0, wtmRdr1.getImgWidth(), wtmRdr1.getImgHeight());
            DataBlk wtmBlk2 = new DataBlkInt(0, 0, wtmRdr2.getImgWidth(), wtmRdr2.getImgHeight());
            
            imgRdr0.getCompData(imgBlk0, 0);
            imgRdr1.getCompData(imgBlk1, 1);
            imgRdr2.getCompData(imgBlk2, 2);
            wtmRdr0.getCompData(wtmBlk0, 0);
            wtmRdr1.getCompData(wtmBlk1, 1);
            wtmRdr2.getCompData(wtmBlk2, 2);

            int level = (int)jDwtLevelSpinner.getValue();

            HaarWT wt0 = new HaarWT((DataBlkInt)imgBlk0, level);
            HaarWT wt1 = new HaarWT((DataBlkInt)imgBlk1, level);
            HaarWT wt2 = new HaarWT((DataBlkInt)imgBlk2, level);
            
            int stegBits = (int)jstegBitsSpinner.getValue();
            
            wt0.applyStegToHH((DataBlkInt)wtmBlk0, stegBits);
            //wt1.applyStegToHH((DataBlkInt)wtmBlk1, stegBits);
            //wt2.applyStegToHH((DataBlkInt)wtmBlk2, stegBits);
            int max = HaarWT.getMaxValue(wt0.getDecomposedImg());
            int min = HaarWT.getMinValue(wt0.getDecomposedImg());
            
            DataBlkInt dec0 = wt0.getDecomposedImg();
            DataBlkInt dec1 = wt1.getDecomposedImg();
            DataBlkInt dec2 = wt2.getDecomposedImg();
            
            Image dImg = DataBlkIntImageProducer
                            .createImage(dec0, dec1, dec2, decomposedImageISP);
            decomposedImageISP.setImage(dImg);

            DataBlkInt src0 = wt0.getSourceImg();
            DataBlkInt src1 = wt1.getSourceImg();
            DataBlkInt src2 = wt2.getSourceImg();
            
            Image sImg = DataBlkIntImageProducer
                            .createImage(src0, src1, src2, originalAndHiddenImageISP);
            originalAndHiddenImageISP.setImage(sImg);
            
//            Image img2 = DataBlkIntImageProducer
//                            .createImage(src0, src1, src2, decomposedImageISP);
//            decomposedImageISP.setImage(img2);
            
        } catch (IOException ex) {
            MsgBox(ex.getMessage());
        }

    }//GEN-LAST:event_jDecomposeButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(final String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(HaarDwtForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(HaarDwtForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(HaarDwtForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(HaarDwtForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {

                HaarDwtForm form;
                
                if (args.length == 2) {
                    form = new HaarDwtForm(args[0], args[1]);
                } else {
                    form = new HaarDwtForm();
                }

                form.setVisible(true);
            }
        });
    }
    
    private void MsgBox(String msg) {

        JOptionPane.showMessageDialog(this, msg);
        
    }

    private String imagePath;
    private String watermarkPath;
    
    private ImgScrollPane originalImageISP;
    private ImgScrollPane originalAndHiddenImageISP;
    private ImgScrollPane decomposedImageISP;
    private ImgScrollPane extractedImageISP;
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jDecomposeButton;
    private javax.swing.JPanel jDecomposedImgPanel;
    private javax.swing.JSpinner jDwtLevelSpinner;
    private javax.swing.JPanel jExtractedImgPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jOriginaAndHiddenlImgPanel;
    private javax.swing.JPanel jOriginalImgPanel;
    private javax.swing.JSpinner jstegBitsSpinner;
    // End of variables declaration//GEN-END:variables
}
